package com.mega.ebcs.integration.processor;



import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import com.ibm.as400.access.Record;
import com.mega.ebcs.ebcsformatter.FieldFormatter;
import com.mega.ebcs.entity.EbcsFormat;
import com.mega.ebcs.entity.EbcsFormatId;
import com.mega.ebcs.service.EbcsService;
import com.mega.ebcs.util.ApplicationContextUtils;
import com.mega.ebcs.util.Constant;

public class ClientToHostProcessor implements Processor{
	
	@Override
	public void process(Exchange exchange) throws Exception {
		EbcsService ebcsService = (EbcsService) ApplicationContextUtils.getApplicationContext().getBean("ebcsService");
		String fileName = exchange.getIn().getMessageId().replace("-", "").replace(":", "");
		exchange.getIn().setHeader("fileName", fileName);
		FieldFormatter ff = new FieldFormatter();
		Record record = ff.packAsciiToEbcs(exchange.getIn().getBody().toString(), new Long(1), Constant.EXCHANGE_REQUEST);
		EbcsFormat ebcsFormat = ebcsService.findEbcsFormatById(new EbcsFormatId(Constant.FIELDNAME_ID, Constant.EXCHANGE_REQUEST));		
		ff.setEbcsField(record, fileName, ebcsFormat);
		
		exchange.getIn().setBody(record.getContents());
	}

}
